package com.open.pokergames.base.activity;

import android.content.Context;
import android.os.Bundle;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;

import com.gyf.immersionbar.ImmersionBar;
import com.open.pokergames.comm.repository.cache.LocalCache;
import com.open.pokergames.comm.repository.entity.ResultCode;
import com.open.pokergames.comm.repository.entity.ResultInfo;
import com.open.pokergames.comm.userCenter.WelcomeActivity;
import com.sinothk.android.utils.XUtils;
import com.sinothk.dialog.loading.LoadingDialog;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
@Deprecated
public abstract class BaseActivity extends AppCompatActivity {

    private LoadingDialog loadingDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 标题处理
        ImmersionBar.with(this)
                .keyboardEnable(true)
                .keyboardMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE)
                .init();
        XUtils.page().addActivity(this);
    }

    public void tip(String msg) {
        XUtils.toast().show(msg);
    }

    public void showLoadingDialog(String msg) {
        loadingDialog = new LoadingDialog(this);
        loadingDialog.show(false, msg);
    }

    public void hideLoadingDialog() {
        if (loadingDialog != null) {
            loadingDialog.dismiss();
            loadingDialog = null;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (loadingDialog != null) {
            loadingDialog.dismiss();
            loadingDialog = null;
        }
    }

    public void softInputFocusable(EditText editText) {
        editText.setFocusable(true);
        editText.setFocusableInTouchMode(true);
        editText.requestFocus();

        InputMethodManager inputManager = (InputMethodManager) editText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        assert inputManager != null;
        inputManager.showSoftInput(editText, 0);
    }

    @Override
    protected void onResume() {
        super.onResume();
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void globalCallback(ResultInfo<String> result) {
        if (!ResultInfo.EVENT_TYPE_GLOBAL.equals(result.getEventType())) {
            return;
        }

        if (ResultCode.SYS_INVALID_SIGN.code().equals(result.getCode())) {
            XUtils.toast().show("登录已过期,请重新登录");
            LocalCache.logout();
            XUtils.page().finishAll();
            XUtils.intent().openActivity(this, WelcomeActivity.class)
                    .start();
        }
    }

}
